package com.fqgj.sentry.manage;


import com.fqgj.common.utils.CollectionUtils;
import com.fqgj.sentry.common.exception.BizException;
import com.fqgj.sentry.common.exception.enums.ErrorCodeEnums;
import com.fqgj.sentry.common.utils.ApiPage;
import com.fqgj.sentry.manage.dao.McGroupManagerDao;
import com.fqgj.sentry.manage.dao.McRoleDao;
import com.fqgj.sentry.manage.dao.McRolePermissionDao;
import com.fqgj.sentry.manage.entity.McGroupManagerEntity;
import com.fqgj.sentry.manage.entity.McRoleEntity;
import com.fqgj.sentry.manage.service.RoleService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;


/**
 * Created by ykpbean kangping.ying@yuntu-inc.com
 *
 * @description
 * @create 2017-07-13 下午5:38
 */

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private McRoleDao mcRoleDao;
    @Autowired
    private McRolePermissionDao mcRolePermissionDao;
    @Autowired
    private McGroupManagerDao mcGroupManagerDao;

    @Override
    public List<McRoleEntity> selectByMerchantForPage(Long merchantId, ApiPage apiPage) {
        HashMap<String, Object> param = new HashMap<>();
        param.put("merchantId", merchantId);
        Page<McRoleEntity> page = PageHelper.startPage(apiPage.getPageNum(), apiPage.getPageSize());
        mcRoleDao.selectRoleList(param);
        apiPage.setTotal(page.getTotal());
        return page;
    }

    @Override
    public List<McRoleEntity> selectListByMerchant(Long merchantId) {
        HashMap<String, Object> param = new HashMap<>();
        param.put("merchantId", merchantId);
        List<McRoleEntity> roleEntityList = mcRoleDao.selectRoleList(param);
        return roleEntityList;
    }

    /**
     * 角色删除
     *
     * @param roleId
     * @return
     */
    @Override
    public Integer deleteRole(Long roleId) {
        //删除角色时需要
        McRoleEntity roleEntity = mcRoleDao.selectByPrimaryKey(roleId);
        if (null == roleEntity || roleEntity.getDeleted()) {
            throw new BizException(ErrorCodeEnums.role_is_not_exist_error);
        }
        List<McGroupManagerEntity> groupManagerEntities = mcGroupManagerDao.findByRoleId(roleId);
        if (CollectionUtils.isNotEmpty(groupManagerEntities)) {
            throw new BizException(ErrorCodeEnums.role_have_user_error);
        }
        //删除角色
        mcRoleDao.deleteByPrimaryKey(roleId);
        //删除角色对应的  权限
        return mcRolePermissionDao.deleteByRoleId(roleId);
    }

    /**
     * 新增角色
     *
     * @param roleEntity
     * @return
     */
    @Override
    public McRoleEntity insert(McRoleEntity roleEntity) {
        return mcRoleDao.insert(roleEntity);
    }


    /**
     * 修改 角色
     *
     * @param roleEntity
     * @return
     */
    @Override
    public Integer update(McRoleEntity roleEntity) {
        return mcRoleDao.updateByPrimaryKey(roleEntity);
    }
}
